import cgi
import os
import logging
import base64
import authorization

from collections import deque
from xml.etree import ElementTree as ET
from google.appengine.ext.webapp import template
from models import model
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
class Boards(webapp.RequestHandler):

    def post(self):

        headers = self.request.headers
        logging.error(headers)
        user = authorization.authorization(headers)
        logging.error(user)
        if(user == ""):
          self.error(401)
        else:
          root = ET.XML(self.request.body)
          create_board = root[0]

          find_name = create_board.find("name")
          find_owner = create_board.find("owner")
          find_invitee = create_board.find("invitee")
          find_size = create_board.find("size")
          find_owner_white = create_board.find("owner_white")
          
          name = find_name.attrib.get("value")
          owner = find_owner.attrib.get("value")
          if(find_invitee != None):
            invitee = find_invitee.attrib.get("value")
            logging.error(invitee)
            if(invitee != None):
              status = "FULL"
            else:
              status = "OPEN"
          else:
            status = "OPEN"
          if(find_size != None):
            size = find_size.attrib.get("value")
          else:
            size = "9x9"
          if(find_owner_white != None):
            owner_white = find_owner_white.attrib.get("value")
          else:
            owner_white = "true"

          current_board = [[6,6,6,6,6,6,6,6,6,6,6],[6,0,0,0,0,0,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,2,2,2,2,2,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,0,0,0,0,0,0,0,0,0,6],[6,6,6,6,6,6,6,6,6,6,6]]

          #parameter = model.Parameters()
          #parameter.Value = str(tableau)
          #eval(str)
          #parameter.Name = "test"
          #parameter.put()
          #test = model.Parameters.all()

          board = model.Boards.all()

          board_name = board.filter('Name = ', name).fetch(limit=1)

          if(board_name != []):
            self.error(403)
          else:
            if(not model.Users.all().filter("Full_name =", owner)):
              self.error(403)
              return 0
            if(status == "FULL"):
             if(not model.Users.all().filter("Full_name =", invitee)):
                self.error(403)
                return 0
            else:
              invitee = ""

            board = model.Boards()
            board.Name = name
            board.Owner = owner
            board.Invitee = invitee
            board.Size = size
            board.Status = status
            board.Current_board = str(current_board)
            board.Owner_is_white = owner_white
            board.put()

    def get(self):

        start = self.request.get("start")
        end = self.request.get("end")
        last = self.request.get("last")
        
        boards = model.Boards.all().order("Created_at")
        count = boards.count()

        if(last != "" and (start != "" or end != "")):
          self.response.clear()
          self.response.set_status(406) #not acceptable
        else:
          if(end != "" and start == ""):
            boards = boards.fetch(limit=int(end))
          if(start != "" and end == ""):
            boards = boards.fetch(limit=count,offset=int(start)-1)
          if(last != ""):
            boards = boards.fetch(limit=count, offset=count - int(last))
          if(start != "" and end != ""):
            boards = boards.fetch(limit=int(end)-int(start)+1,offset=int(start)-1)

        template_values = {
          'boards': boards,
        }

        path = os.path.join(os.path.dirname(__file__), 'templates/boards.html')

        self.response.headers['Content-Type'] = 'text/xml'
        self.response.out.write(template.render(path, template_values))
